package com.enation.app.web.core.plugin.homepage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.enation.app.base.core.model.PluginDataMapper;
import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;
import com.enation.framework.plugin.AutoRegisterPluginsBundle;
import com.enation.framework.plugin.IPlugin;

/**
 * 
 * @function 首页设置插件桩
 * @author 闯哥
 * @time 2016年10月19日 上午9:21:24
 */
@Service("homePagePluginBundle")
public class HomePagePluginBundle extends AutoRegisterPluginsBundle {

	@Override
	public String getName() {

		return "首页设置插件桩";
	}

	@Override
	public synchronized void registerPlugin(IPlugin plugin) {
		// TODO Auto-generated method stub
		super.registerPlugin(plugin);
	}

	public List<PluginDataMapper> getTabList() {
		List<PluginDataMapper> list = new ArrayList<PluginDataMapper>();

		List<IPlugin> plugins = this.getPlugins();

		if (plugins != null) {

			for (IPlugin plugin : plugins) {

				if (plugin instanceof IHomePageTabShowEvent) {
					PluginDataMapper dataMapper = new PluginDataMapper();
					IHomePageTabShowEvent event = (IHomePageTabShowEvent) plugin;

					dataMapper.setOrder(event.getOrder());
					dataMapper.setTabTitle(event.getTabName());
					list.add(dataMapper);
				}

			}
		}

		return list;
	}

	public List<PluginDataMapper> getDetailHtml() {
		List<PluginDataMapper> list = new ArrayList<PluginDataMapper>();

		FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
		List<IPlugin> plugins = this.getPlugins();

		if (plugins != null) {

			for (IPlugin plugin : plugins) {

				if (plugin instanceof IHomePageTabShowEvent) {

					PluginDataMapper dataMapper = new PluginDataMapper();
					IHomePageTabShowEvent event = (IHomePageTabShowEvent) plugin;

					freeMarkerPaser.setClz(event.getClass());
					freeMarkerPaser.setPageName(event.onShow());

					dataMapper.setOrder(event.getOrder());
					dataMapper.setTabHtml(freeMarkerPaser.proessPageContent());

					list.add(dataMapper);
				}
			}
		}

		return list;
	}

}
